package com.proactive.webapp.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.proactive.webapp.dao.ActivityDAO;
import com.proactive.webapp.dao.ActivityImageDAO;
import com.proactive.webapp.dao.CardTemplateDAO;
import com.proactive.webapp.dao.MenuDAO;
import com.proactive.webapp.entity.Activity;
import com.proactive.webapp.entity.ActivityImage;
import com.proactive.webapp.entity.CardTemplate;
import com.proactive.webapp.entity.Manager;
import com.proactive.webapp.entity.Member;
import com.proactive.webapp.entity.Menu;
import com.proactive.webapp.util.DateConversion;
import com.proactive.webapp.util.FileUtil;
import com.proactive.webapp.util.MenuUtil;
import com.proactive.webapp.util.PageCount;


@Controller
public class ActivityController {
	ApplicationContext context =  new ClassPathXmlApplicationContext("spring-module.xml");
		
	@RequestMapping(value = "/showActivity", method = RequestMethod.GET)
	public ModelAndView getList(HttpServletRequest request, HttpSession session) {
		ModelAndView model = new ModelAndView();
		//import DAO
		
		ActivityDAO activityDAO = (ActivityDAO) context.getBean("activityDAO");
		CardTemplateDAO cardTemplateDAO = (CardTemplateDAO) context.getBean("cardTemplateDAO");
		
		//get data from request
		int pageNow = request.getParameter("p")==null?1:Integer.parseInt(request.getParameter("p"));
		int activityID = 1;
		//set pager
		int pageSize = 30;
		int count = cardTemplateDAO.countTotalByStatus(activityID, 1);
		String pageUrl = "showActivity";
		String pageEndUrl1 = "";
		PageCount pageCount = new PageCount(pageNow, pageSize, count, pageUrl, pageEndUrl1);
		pageCount.calculate();
		
		Activity activity = new Activity();
		activity.setId(activityID);
		activity.setStatus(1);
		activity = activityDAO.getByStatus(activity);
		
		List<CardTemplate> cardTemplateList = new ArrayList<CardTemplate>();
		cardTemplateList = cardTemplateDAO.getListByStatus(activityID, 1, pageCount.getStart(), pageSize);

		List<Menu> menuList = MenuUtil.getList(((MenuDAO)context.getBean("menuDAO")).getList(), 5);
		
		//set model
		model.addObject("page", pageCount);
		model.addObject("activity", activity);
		model.addObject("cardTemplateList", cardTemplateList);
		model.addObject("menuList", menuList);
		
		//check login
		Member loginMember = (Member)session.getAttribute("loginMember");
		model.setViewName(loginMember!=null?"activityDetail1":"activityDetail2");
		return model;
	}
	
	@RequestMapping(value = "/management/modiActivityCont", method = RequestMethod.GET)
	public ModelAndView modiActivityCont(Activity activity, ActivityImage activityImage, HttpSession session){
		int activityID = 1;
		ModelAndView model = new ModelAndView();

		Manager loginManager = (Manager)session.getAttribute("loginManager");
		if(loginManager != null){
			ActivityDAO activityDAO = (ActivityDAO)context.getBean("activityDAO");
			ActivityImageDAO activityImageDAO = (ActivityImageDAO)context.getBean("activityImageDAO");
			List<ActivityImage> activityImageList = new ArrayList<ActivityImage>();
			activity.setId(activityID);
			activityImage.setAid(activityID);
			activityImageList=activityImageDAO.getList(activityID);
			
			activity=activityDAO.get(activity);
			
			model.addObject("activity",activity);
			model.addObject("activityImageList",activityImageList);
			model.setViewName("/management/modiActivity");

		}
		else
			model.setViewName("redirect:/management/login");
		
		return model;
	}
	
	
	@RequestMapping(value = "/management/modiActivity", method = RequestMethod.POST)
	public ModelAndView modiActivity(@ModelAttribute Activity activity, HttpSession session) {
		ModelAndView model = new ModelAndView();
		Manager loginManager = (Manager)session.getAttribute("loginManager");
		if(loginManager != null){
			ActivityDAO activityDAO = (ActivityDAO)context.getBean("activityDAO");
			Activity oldActivity=new Activity();
			oldActivity.setId(activity.getId());
			oldActivity=activityDAO.get(oldActivity);
			String path = session.getServletContext().getRealPath("/");
			String path1 = session.getServletContext().getRealPath("resources/image/activity/banner");
			String path2 = session.getServletContext().getRealPath("resources/image/activity/share");
		
			DateConversion dateConversion = new DateConversion();
			Date now = new Date();
		
			if(activity.getBanner().getSize() > 0){
				FileUtil fileUtil = new FileUtil(activity.getBanner().getOriginalFilename());
				String newFileName = dateConversion.ConversionYMDHMS2(now) + "." + fileUtil.getFileType();
				File targetFile = new File(path1, newFileName);
		        if(!targetFile.exists()){
		            targetFile.mkdirs();
		        }
		        try {
		        	activity.getBanner().transferTo(targetFile);
		        } catch (Exception e) {
		            e.printStackTrace();
		        }
		        
		        if(!oldActivity.getBannerPath().equals("")){
		        	File oldFile = new File(path+oldActivity.getBannerPath());
		        	oldFile.delete();
		        }
		        
		        activity.setBannerPath("resources/image/activity/banner/"+newFileName);
			
			}
			else
				activity.setBannerPath(oldActivity.getBannerPath());
			
			
			if(activity.getShareImage().getSize() > 0){
				FileUtil fileUtil = new FileUtil(activity.getShareImage().getOriginalFilename());
				String newFileName = dateConversion.ConversionYMDHMS2(now) + "." + fileUtil.getFileType();
				File targetFile = new File(path2, newFileName);
		        if(!targetFile.exists()){
		            targetFile.mkdirs();
		        }
		        try {
		        	activity.getShareImage().transferTo(targetFile);
		        } catch (Exception e) {
		            e.printStackTrace();
		        }
		        
		        if(!oldActivity.getShareImagePath().equals("")){
			        File oldFile = new File(path+oldActivity.getShareImagePath());
		        	oldFile.delete();
		        }
		        
		        activity.setShareImagePath("resources/image/activity/share/"+newFileName);
			}
			
			else
				activity.setShareImagePath(oldActivity.getShareImagePath());
	       
			activityDAO.update(activity);
			oldActivity=activityDAO.get(activity);
			model.setViewName("redirect:/management/modiActivityCont");
		}
		else
			model.setViewName("redirect:/management/login");
		
		return model;
	
	}
}
